package com.ruoyi.cs.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.TRoll;
import com.ruoyi.cs.domain.TUser;
import com.ruoyi.cs.domain.vo.TRollCyVo;
import com.ruoyi.cs.mapper.TGoodsMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.mapper.TRollMapper;
import com.ruoyi.cs.mapper.TUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.TRollCyMapper;
import com.ruoyi.cs.domain.TRollCy;
import com.ruoyi.cs.service.ITRollCyService;

/**
 * ROLL房间参与Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-25
 */
@Service
@SuppressWarnings("all")
public class TRollCyServiceImpl implements ITRollCyService 
{
    @Autowired
    private TRollCyMapper tRollCyMapper;
    @Autowired
    private TGoodsMapper tGoodsMapper;
    @Autowired
    private TRollMapper tRollMapper;
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * 查询ROLL房间参与
     * 
     * @param cyId ROLL房间参与ID
     * @return ROLL房间参与
     */
    @Override
    public TRollCy selectTRollCyById(String cyId)
    {
        return tRollCyMapper.selectTRollCyById(cyId);
    }

    /**
     * 查询ROLL房间参与列表
     * 
     * @param tRollCy ROLL房间参与
     * @return ROLL房间参与
     */
    @Override
    public List<TRollCy> selectTRollCyList(TRollCy tRollCy)
    {
        return tRollCyMapper.selectTRollCyList(tRollCy);
    }

    /**
     * 新增ROLL房间参与
     * 
     * @param tRollCy ROLL房间参与
     * @return 结果
     */
    @Override
    public int insertTRollCy(TRollCy tRollCy)
    {
        tRollCy.setCreateTime(DateUtils.getNowDate());
        return tRollCyMapper.insertTRollCy(tRollCy);
    }

    /**
     * 修改ROLL房间参与
     * 
     * @param tRollCy ROLL房间参与
     * @return 结果
     */
    @Override
    public int updateTRollCy(TRollCy tRollCy)
    {
        tRollCy.setUpdateTime(DateUtils.getNowDate());
        return tRollCyMapper.updateTRollCy(tRollCy);
    }

    /**
     * 批量删除ROLL房间参与
     * 
     * @param cyIds 需要删除的ROLL房间参与ID
     * @return 结果
     */
    @Override
    public int deleteTRollCyByIds(String[] cyIds)
    {
        return tRollCyMapper.deleteTRollCyByIds(cyIds);
    }

    /**
     * 删除ROLL房间参与信息
     * 
     * @param cyId ROLL房间参与ID
     * @return 结果
     */
    @Override
    public int deleteTRollCyById(String cyId)
    {
        return tRollCyMapper.deleteTRollCyById(cyId);
    }

    /**
     * 后台ROLL房奖品记录
     * @param tRollCyVo
     * @return
     */
    @Override
    public List<TRollCyVo> prizeListBack(TRollCyVo tRollCyVo) {
        tRollCyVo.setDelFlag(Constants.SUCCESS);
        return tRollCyMapper.prizeListBack(tRollCyVo);
    }

    /**
     * 后台ROLL房参与记录
     * @param tRollCyVo
     * @return
     */
    @Override
    public List<TRollCyVo> partakeRecodeList(TRollCyVo tRollCyVo) {
        return tRollCyMapper.prizeListBack(tRollCyVo);
    }

    /**
     * 批量指定开奖饰品
     * @param list
     * @return
     */
    @Override
    public Response batchAppointGoodsId(List<TRollCy> list) {
        if (list == null || list.size() == 0) {
            return Response.fail(Constants.FAILCODE,"参数不能为空！");
        }
        /**判断ROLL房中是否已存在已指定的饰品*/
        for (TRollCy tRollCy : list) {
            TRoll tRoll = tRollMapper.selectTRollById(tRollCy.getRollId());
            if (tRoll.getRollStatus().equals(Constants.ROLLSTATUS2)) {
                return Response.fail(Constants.FAILCODE,"ROLL房已结束！");
            }

            Long num = tRollCyMapper.isAppointGoods(tRollCy.getRollId(),tRollCy.getAppointGoodsId());
            if (num > 0){
                return Response.fail(Constants.FAILCODE,"重复指定开奖的饰品！");
            }
        }
        int row = tRollCyMapper.updateAppointGoodsId(list);
        return row > 0 ? Response.success() : Response.error();
    }

    /**
     * ROLL房指定饰品列表
     * @param goodsBelongingId
     * @return
     */
    @Override
    public List<Map> appointGoodsList(String goodsBelongingId) {
        return tRollCyMapper.appointGoodsList(goodsBelongingId);
    }

    /**
     * 添加ROLL房指定用户
     * @param userPhone
     * @param rollId
     * @return
     */
    @Override
    public Response addAppointUser(String userPhone,String rollId) {
        if (StringUtils.isEmpty(userPhone)){
            return Response.fail(Constants.FAILCODE,"用户手机号必填!");
        }
        TUser tuser = tUserMapper.getUserInfoByPhone(userPhone);
        if (tuser == null){
            return Response.fail(Constants.FAILCODE,"用户不存在!");
        }
        if (!tuser.getDelFlag().equals(Constants.SUCCESS)){
            return Response.fail(Constants.FAILCODE,"用户已冻结或者删除!");
        }
        /**判断用户是否参与该ROLL房间*/
        Long userCount = tRollCyMapper.selectTRollCyUserCount(tuser.getUserId(), rollId);
        if (userCount > 0l) {
            return Response.fail(Constants.FAILCODE,"用户已参与！");
        }
        TRoll tRoll = tRollMapper.selectTRollById(rollId);
        String num = tRoll.getCyNumberlimit();
        if (StringUtils.isNotEmpty(num) && tRoll.getDrawPriceType().equals("1")) {
            Map<String, Long> ma = tRollCyMapper.countUserNumByRollId(tRoll.getRollId());
            if (Long.parseLong(num) <= ma.get("userNum")) {
                return Response.fail(Constants.FAILCODE, "参与人数已达上限！");
            }
        }
        /**插入ROLL房参与表*/
        TRollCy tRollCy = new TRollCy();
        tRollCy.setUserId(tuser.getUserId());
        tRollCy.setRollId(rollId);
        tRollCy.setCreateTime(DateUtils.getNowDate());
        tRollCy.setDelFlag(Constants.FAIL);//默认插入没中奖
        tRollCy.setIsAppointUser("1");
        tRollCyMapper.insertTRollCy(tRollCy);
        return Response.success("指定用户添加成功");
    }
}
